/*
 *
 *方法的整理与重构
 *
 *
 */

//定义app参数名称，并加载其它模块
var authApp=angular.module('authapp', ["ngCookies",'ngFileUpload','ui.bootstrap']);

//此为定位页面所用
authApp.value('page','opinion_list');

/*controller部分*/

//主要操作controller
authApp.controller("opinionEditController",function($scope,$http,$getQueryString){
    $scope.listsFirst = '';
    $scope.listLevel1 = '';
    $scope.itemId3 = '';
    $scope.modes = '';
    $scope.analysis = '';
    $scope.problem = '';
    $scope.suggest = '';
    $scope.keys = '';
    $scope.values = '';
    $scope.schoolId=$getQueryString("schoolId");
    $scope.itemId=$getQueryString("itemId");
    $scope.index=$getQueryString("index");
    $scope.firstName=$getQueryString("firstName")
    $scope.schoolName=$getQueryString("schoolName")
    $scope.state = Math.ceil($getQueryString("state"))
    $scope.down=function() {
        return window.open('/views/investiga/table8.html?schoolId='+ $scope.schoolId+'&itemId='+ $scope.itemId + '&index=' + $scope.index + '&firstName='+ $scope.firstName + '&schoolName='+$scope.schoolName)
    }
    $scope.firstName=unUnicode($scope.firstName);
    $scope.schoolName=unUnicode($scope.schoolName);
    $scope.type = false
    $scope.queryMain=function(){
        $http({
            method:'post',
            url:apis.querySurveyOpinionData,
            data:{'schoolId':$scope.schoolId,'itemId':$scope.itemId},
        }).success(function(response){
            if(response.code=="000000"){
                $scope.ListsData(response);
                
            }else{
                alert(response.errorMsg);
            }
        }).error(function(){
            alert("网络出现故障，请确认网络是否连接");
        })
    }
    $scope.queryMain();

    $scope.problemData = function(){
        $http({
            method:'post',
            url:apis.querySurveyProblemData,
            data:{'schoolId':$scope.schoolId,'itemId':$scope.itemId},
        }).success(function(response){
            if(response.code=="000000"){
                if(response.data) {
                    $scope.analysis = response.data.s_analysis;
                    $scope.problem = response.data.s_problem;
                    $scope.suggest = response.data.s_suggest;
                }
            }else{
                alert(response.errorMsg);
            }
        }).error(function(){
            alert("网络出现故障，请确认网络是否连接");
        })
    }
    $scope.problemData();
    //调用主接口成功之后的处理方法
    $scope.queryMainApiCallBack=function(response){
        if(response.code=="000000"){

            var arraylevels1= [];
            var arraydetails1= [];
            var listnum = 0;

            angular.forEach(response.data, function (data, index, array) {
                if(data){
                    arraylevels1.push(data.s_key);
                    arraydetails1.push(data.s_value);
                }
            })
            console.log($scope.listsFirst)
            angular.forEach($scope.listsFirst, function (obj, index, array) {
                if (obj) {
                    var anum = 0;
                    var bnum = 0;
                    var cnum = 0;
                    var cont = 0;
                    angular.forEach(obj.list, function (data, index, array) {
                        data.level = arraylevels1[listnum];
                        data.detail = arraydetails1[listnum];

                        if (arraylevels1[listnum] == "A") {
                            anum++;
                        } else if (arraylevels1[listnum] == "B") {
                            bnum++;
                        } else if (arraylevels1[listnum] == "C") {
                            cnum++;
                        } else {
                            cont++;
                        }
                        listnum++;
                    })

                    if (cont != 0) {
                        $scope.listLevel1 = "";
                    } else if (cnum > 0) {
                        $scope.listLevel1 = "C";
                    } else if (bnum > 0 && cnum == 0) {
                        $scope.listLevel1 = "B";
                    }
                    else if (bnum == 0 && cnum == 0) {
                        $scope.listLevel1 = "A";
                    }

                    obj.listLevel = $scope.listLevel1;
                }
            })


        }else{
            alert(response.errorMsg);
        }
    }

    //一级指标三级查询
    $scope.ListsData=function(res){
        $http({
            method:'post',
            url:apis.queryDetailedData,
            data:{'itemId':$scope.itemId, schoolId: $scope.schoolId},
        }).success(function(response){
            if(response.code=="000000"){
                $scope.listsFirst =  response.data;
                angular.forEach( $scope.listsFirst,function(data,index,array){
                    if(data){
                        data.listLevel = $scope.listLevel1;

                        angular.forEach(data.list,function(data,index,array){
                            //data.level  = "level"+data.s_number;
                            data.level  = "";
                            data.detail = "";
                            $scope.itemId3 += (data.p_id + "#");
                            $scope.modes += (data.s_number + "#");

                        })
                        $scope.queryMainApiCallBack(res);
                    }
                })
            }else{
                alert(response.errorMsg);
            }
        }).error(function(){
            alert("网络出现故障，请确认网络是否连接");
        })
    }
   // $scope.ListsData();


    //根据细则值计算二级指标等级
    $scope.showResult=function(obj,listsSecond){
        var anum = 0;
        var bnum = 0;
        var cnum = 0;
        var count = 0;
       
        angular.forEach(obj,function(data,index,array){
           
            if(data.grade == "A"){
                data.detail = ''
                anum++;
            }else if(data.grade == "B"){
                bnum++;
            }else if(data.grade == "C"){
                cnum++;
            }else{
                count++;
            }
           
        })

        if(count != 0){
            $scope.listLevel1 = "";
        }else if(cnum > 0){
            $scope.listLevel1 = "C";
        }else if(bnum > 0 && cnum == 0 ){
            $scope.listLevel1 = "B";
        }
        else if(bnum == 0 && cnum == 0 ){
            $scope.listLevel1 = "A";
        }

        listsSecond.listLevel = $scope.listLevel1;

    }

    //返回到前页面
    $scope.goback=function(){
       // if(window.confirm("是否确认返回？")){
            javascript:history.back(-1);
       // }
    }

    $scope.saveData=function(){
        angular.forEach($scope.listsFirst,function(obj,index,array){
            if(obj && !$scope.type){
                angular.forEach(obj.list,function(data,index,array){
                    if(!data.grade){
                        $scope.keys += ("无"+"#");
                    }else{
                        $scope.keys += (data.grade+"#");
                    }

                    if(!data.detail){
                        $scope.values += ("无"+"#");
                    }else{
                        $scope.values += (data.detail+"#");
                    }

                })
            }
        })

        if(window.confirm("是否确认保存？")){
            var rJson={
                schoolId:$scope.schoolId,
                itemId:$scope.itemId3,
                keys:$scope.keys,
                values:$scope.values,
                modes:$scope.modes,
                itemId1:$scope.itemId,
                analysis:$scope.analysis,
                problem:$scope.problem,
                suggest:$scope.suggest,
            }

            $http({
                method:'post',
                url:apis.saveSurveyOpinionData,
                data:rJson,
            }).success(function(response){
                if(response.code=="000000"){
                    // $scope.queryMain();
                    // $scope.problemData();
                    alert("保存数据成功！");
                    $scope.type = false
                    window.location.reload();
                }else{
                    $scope.type = true
                    alert(response.errorMsg);
                }
            }).error(function(){
                $scope.type = true
                alert("网络出现故障，请确认网络是否连接");
            })
        }
    }
})
function unUnicode(str)
{
    return unescape(str.replace(/\/\//g, "%"));
}
function toUnicode(str)
{
	return escape(str).replace(/%/g,"//").toLowerCase();
}